package imagingbook.sift;

import java.util.Formatter;
import java.util.Locale;

import ij.IJ;

public class SiftDescriptor {
	
	public double x;	// image position
	public double y;
	public double scale;
	public double orientation;
	public int[] features;

	public SiftDescriptor(double x, double y, double scale, double orientation, int[] fi) {
		this.x = x;
		this.y = y;
		this.scale = scale;
		this.orientation = orientation;
		this.features = fi;
	}
	
	public void print() {
		IJ.log(String.format(Locale.US, "%.2f %.2f %.2f %.2f", x, y, scale, orientation));
		printFeatures();
	}
	
	// -----------------------------
	
	public double getDistanceL1(SiftDescriptor other) {
		int[] f1 = this.features;
		int[] f2 = other.features;
		int sum = 0;
		for (int i=0; i<f1.length; i++) {
			sum = sum + Math.abs(f1[i] - f2[i]);
		}
		return sum;
	}
	
	public double getDistanceL2(SiftDescriptor other) {
		int[] f1 = this.features;
		int[] f2 = other.features;
		int sum = 0;
		for (int i=0; i<f1.length; i++) {
			int d = f1[i] - f2[i];
			sum = sum + d * d;
		}
		return Math.sqrt(sum);
	}
	
	public double getDistanceLinf(SiftDescriptor other) {
		int[] f1 = this.features;
		int[] f2 = other.features;
		int dmax = 0;
		for (int i=0; i<f1.length; i++) {
			int d = Math.abs(f1[i] - f2[i]);
			dmax = Math.max(dmax, d);
		}
		return dmax;
	}
	
	// -----------------------------
	
	void printFeatures() {
		int linelength = 16;
		StringBuilder sb = new StringBuilder();
		Formatter fm = new Formatter(sb, Locale.US);
		for (int i = 0; i < features.length; i++) {
			if (i > 0 && i % linelength == 0) {
				IJ.log(sb.toString());
				sb.setLength(0);
			}
			fm.format(" %3d", features[i]);	// 0 <= features[i] < 256
		}
		IJ.log(sb.toString());
	}

}
